<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Classes extends AdminBase
{
    protected $class_model;
    protected $admin_user_model;
    protected $student_model;

    public function __construct()
    {
        parent::__construct();
        $this->class_model = new \app\common\model\Classes();
        $this->admin_user_model = new \app\common\model\AdminUser();
    }

    /**
     * @param $keyword
     * @param $page
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($keyword = '', $page = 1){
        $map = [];

        if (!empty($keyword)) {
            $map['class_name'] = ['like', "%{$keyword}%"];
        }

        $class_list = $this->class_model->where($map)->order('id DESC')
            ->paginate(10, false, ['page' => $page])->each(function($item, $key){
                $user_id = $item['user_id'];
                $user = $this->admin_user_model->find($user_id);
                $item['username'] = $user['username'];
            });
        return $this->fetch('index', ['class_list' => $class_list,'keyword' => $keyword]);
    }

    /**
     * 添加班级
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()) {

            $data = $this->request->post();
            $data['user_id'] = session('admin_id');

            $validate_result = $this->validate($data, 'Classes');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $result = $this->class_model->allowField(true)->save($data);
                if ($result) {
                    $this->success("保存成功");
                } else {
                    $this->error("保存失败");
                }
            }
        } else {
            return $this->fetch();
        }
    }

    /**
     * 删除班级
     * @param $id
     * @return void
     */
    public function delete($id = 0)
    {
        if ($id === 0) {
            $this->error("删除失败！err-1");
        } else {
            $result = $this->class_model->destroy($id);
            if ($result) {
                $this->success("删除成功");
            } else {
                $this->success("删除失败！err-2");
            }
        }
    }

    /**
     * 编辑班级
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate_result = $this->validate($data, 'Classes');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $result = $this->class_model->allowField(true)->save($data,['id' => $data['id']]);
                if ($result !== false) {
                    $this->success("保存成功");
                } else {
                    $this->error("保存失败");
                }
            }
        } else {
            $class = $this->class_model->find($id);
            $this->assign('class', $class);
            return $this->fetch();
        }
    }
}